// Load all images here:
var lofi_char_image = new Image();
lofi_char_image.src = "lofi_char.png";
var lofi_env_image = new Image();
lofi_env_image.src = "lofi_environment.png";
var lofi_df_image = new Image();
lofi_df_image.src = "lofi_dungeon_features.png";
var lofi_if_image = new Image();
lofi_if_image.src = "lofi_interface.png";

// Dimensions:
var text_width = 32;
var text_height = 32;

var sprites = {
  'mouse' : { img : lofi_char_image, x : 0, y : 384 },
  'gray_mouse' : { img : lofi_char_image, x : 32, y : 384 },
  'floor' : { img : lofi_env_image, x : 160, y : 64 },
  'wall' : { img: lofi_env_image, x : 320, y : 288 },
  'source' : { img: lofi_env_image, x : 384, y : 224 },
  'dirt' : { img : lofi_env_image, x : 192, y : 32 },
  'goal' : { img : lofi_env_image, x : 352, y : 64 },
  'goal_closed' : { img : lofi_env_image, x : 320, y : 64 },
  'lava1' : { img : lofi_env_image, x : 0, y : 224 },
  'lava2' : { img : lofi_env_image, x : 0, y : 256 },
  'switch_on' : { img : lofi_env_image, x : 448, y : 320 },
  'switch_off' : { img : lofi_env_image, x : 480, y : 320 },
  'barrier' : { img : lofi_df_image, x : 192, y : 0 },
  'rotator' : { img : lofi_env_image, x : 448, y : 64 },
  'teleport_in' : { img : lofi_env_image, x : 64, y : 320 },
  'teleport_out' : { img : lofi_env_image, x : 96, y : 320 },
  'staircase' : { img : lofi_env_image, x : 224, y : 32 },
  'slash' : { img : lofi_if_image, x : 388, y : 223 },
  'skull' : { img : lofi_if_image, x : 32, y : 64 },
  'white_mouse' : { img : lofi_if_image, x : 480, y : 0 },
};

function drawSprite(c2d, sprite_name, x, y) {
  var sprite_def = sprites[sprite_name];
  var w = 32;
  var h = 32;
  if (sprite_def.w) {
    w = sprite_def.w;
  }
  if (sprite_def.h) {
    h = sprite_def.h;
  }
  c2d.drawImage(sprite_def.img, sprite_def.x, sprite_def.y, w, h, x, y, w, h);
}

function drawNumber(c2d, num, padding, x, y) {
  num = '' + num;
  for (var i = 0; i < padding - num.length; ++i) {
    c2d.drawImage(lofi_if_image, 128, 192, text_width, text_height,
                  x, y, text_width, text_height);
    x += text_width;
  }
  for (var i = 0; i < num.length; ++i) {
    var ch = num.charAt(i);
    c2d.drawImage(lofi_if_image, 128 + ch * text_width, 192,
                  text_width, text_height, x, y, text_width, text_height);
    x += text_width;
  }
}
